package mt.Junit;

import static org.junit.Assert.*;

import mt.Order;
import mt.dummy.ClientCommDummy;
import mt.dummy.ServerCommDummy;

import org.junit.Before;
import org.junit.Test;

public class JUnitServerCommDummy {
	
	private ServerCommDummy sercomm;
	private ClientCommDummy clicom_one;
	private ClientCommDummy clicom_two;

	@Before
	public void setUp() throws Exception {
		sercomm = new ServerCommDummy();
		clicom_one = new ClientCommDummy();
		clicom_two = new ClientCommDummy();
	}

	@Test
	public void test() {
		sercomm.start();
		clicom_one.connect("host", "Reinaldo");
		clicom_two.connect("host", "Joana");
		try {
			Thread.sleep(200);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		assertTrue(sercomm.clientIsConnected("Joana"));
		assertTrue(sercomm.clientIsConnected("Reinaldo"));
		assertFalse(sercomm.clientIsConnected("Joao"));
		
		
		Order or_buy = Order.createBuyOrder("Joana", "Google", 50, 15);
		clicom_one.sendOrder(or_buy);
		assertTrue(sercomm.getServerMessages().size()!=0);
		
		sercomm.sendError("Reinaldo", "An error as happened!");
		
		sercomm.disconnectClient("Reinaldo");
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		assertFalse(sercomm.clientIsConnected("Reinaldo"));
	}
}
